package com.nlscan.qdkit.ui.publico.component;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputType;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.nlscan.qdkit.ui.publico.R;


public class MyCheckEditView extends ConstraintLayout {
    protected EditText etInput;
    private TextView tvHint;
    private ImageView ivIconStart;
    private ImageView ivIconEnd;

    private View vCutLine;
    private View vCutLineSecond;

    public MyCheckEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public MyCheckEditView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public void setOnInputChangeListener(OnInputChangeListener onInputChangeListener) {
        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                onInputChangeListener.onChange(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.custom_check_edit, this, true);

        etInput = findViewById(R.id.et_input);
        tvHint = findViewById(R.id.tv_hint);
        ivIconStart = findViewById(R.id.iv_icon_start);
        ivIconEnd = findViewById(R.id.iv_icon_end);

        vCutLine = findViewById(R.id.v_cut_line);
        vCutLineSecond = findViewById(R.id.v_cut_line_second);

        //固定单行输入
        etInput.setSingleLine(true);
        etInput.setMaxLines(1);
    }

    private void init(Context context, AttributeSet attrs) {
        initView(context);
        if (attrs == null) {
            return;
        }

        TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.MyCheckEditView);

        setStartIcon(array.getDrawable(R.styleable.MyCheckEditView_startIcon));
        setEndIcon(array.getDrawable(R.styleable.MyCheckEditView_endIcon));
        etInput.setHint(array.getString(R.styleable.MyCheckEditView_hint));
        setPasswordMode(array.getBoolean(R.styleable.MyCheckEditView_isPassword, false));
        setHintVisibility(array.getBoolean(R.styleable.MyCheckEditView_isShowHint, false));

        etInput.setTextColor(array.getColor(R.styleable.MyCheckEditView_textColor, getContext().getResources().getColor(R.color.publico_text_color)));
        etInput.setHintTextColor(array.getColor(R.styleable.MyCheckEditView_textColorHint, getContext().getResources().getColor(R.color.publico_text_color_hint)));
        setSecondBackground(array.getBoolean(R.styleable.MyCheckEditView_isSecondBackground, false));

        array.recycle();
    }

    public void setHint(int res) {
        etInput.setHint(res);
    }

    public void setStartIcon(int res) {
        setStartIcon(getResources().getDrawable(res));
    }

    public void setStartIcon(Drawable drawable) {
        if (drawable == null) {
            ivIconStart.setVisibility(GONE);
            return;
        }

        ivIconStart.setVisibility(VISIBLE);
        ivIconStart.setImageDrawable(drawable);
    }

    public void setEndIcon(int res) {
        setEndIcon(getResources().getDrawable(res));
    }

    public void setEndIcon(Drawable drawable) {
        if (drawable == null) {
            ivIconEnd.setVisibility(GONE);
            return;
        }

        ivIconEnd.setVisibility(VISIBLE);
        ivIconEnd.setImageDrawable(drawable);
    }

    public void setStartClickListener(OnClickListener clickListener) {
        ivIconStart.setOnClickListener(clickListener);
    }

    public void setEndClickListener(OnClickListener clickListener) {
        ivIconEnd.setOnClickListener(clickListener);
    }

    public void setPasswordMode(boolean enable) {
        if (enable) {
            etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {
            etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL);
        }

        etInput.setSelection(etInput.getText().length());
    }

    public void setHintVisibility(boolean isShow) {
        tvHint.setVisibility(isShow ? VISIBLE : GONE);
    }

    public void setSecondBackground(boolean enable) {
        vCutLine.setVisibility(enable ? GONE : VISIBLE);
        vCutLineSecond.setVisibility(enable ? VISIBLE : GONE);
    }

    public void showInfo(String info) {
        tvHint.setText(info != null ? info : "");
    }

    public void showInfo(int res) {
        tvHint.setText(res);
    }

    public void setEditText(String info) {
        etInput.setText(info);
        etInput.setSelection(etInput.getText().length());
    }

    public String getInputText() {
        Editable editable = etInput.getText();
        return editable != null ? editable.toString() : "";
    }

    public interface OnInputChangeListener {
        void onChange(String s);
    }

    public EditText getEtInput() {
        return etInput;
    }

    /**
     * 设置editText hint 文本大小
     * -- 适配hint内容过长，显示不全问题
     *
     * @param textSize dp
     */
    public void setEtHintTextSize(int textSize) {
        SpannableString ss = new SpannableString(etInput.getHint());
        AbsoluteSizeSpan span = new AbsoluteSizeSpan(textSize, true);
        ss.setSpan(span, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        etInput.setHint(new SpannedString(ss));
    }

}
